package com.hdu.other.dp.treeDP;

import jdk.nashorn.internal.ir.Terminal;

public class lc968 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        int ans = 0;
        public int minCameraCover(TreeNode root) {
            int f = f(root);
            if (f == 0) {
                ans++;
            }
            return ans;
        }

        /**
         * 0 - 没有被覆盖
         * 1 - 被覆盖了，上面有摄像头
         * 2 - 被覆盖了，上面没有摄像头
         * @param x
         * @return
         */
        int f(TreeNode x) {
            if (x == null) {
                return 2;
            }
            int l = f(x.left);
            int r = f(x.right);
            if (l == 0 || r == 0) {
                ans++;
                return 1;
            }
            if (l == 1 || r == 1) {
                return 2;
            }
            return 0;
        }
    }
}
